////////////////////////////////////////////////////////////////////////////////
// Company:     SLCC
// Engineers:   Harvey Wilson
// Module name: vga_layers15.v
// from vga_layers.v 26 Aug 2009
//
// Demonstrate combining of various VGA signals in layers.
// The first signal in "if-else" list at end of file is top priority,
// the second signal is next priority, and so on.
//
// Rev 05,10 Sep 2009
// Rev 06 Aug 2010 show how -> added items in sensitivity list to avoid warnings.
////////////////////////////////////////////////////////////////////////////////
module vga_layers15
  (Master_clk, reset,
   // insert other input and output signals here
   stopkey, ascii_kb_tran, arrow,
   number8,
   cur40x15_y_x,
   HSYNC, VSYNC, RGB
   );

   input         Master_clk, reset; // system clock plus reset
   input   [7:0] number8;
   // insert other input and output signals here
   input         stopkey;
   input   [7:0] ascii_kb_tran;
   input   [5:0] arrow; // cursor direction, scale, shift keys
   output [15:0] cur40x15_y_x;
   output HSYNC, VSYNC;
   output  [7:0] RGB;

   // common signal declaration
   reg     [7:0] rgb_next8;

   // instantiate video synchronization module, needed wires first
   // wire reset; // this with next line made box_40x30 into 4 block rams
   // assign reset = 1'b0;
   wire    [7:0] pre_RGB;
   wire    [9:0] pixel_x, pixel_y;
   vga_sync_hw2  VH_SYNC
      (.Master_clk(Master_clk), .reset(reset),
       .pre_RGB(pre_RGB), .RGB(RGB),
       .hsync(HSYNC), .vsync(VSYNC),
       .video_on(), .p_tick(),
       .pixel_x(pixel_x), .pixel_y(pixel_y));

//   // instantiate pong text module, needed wires first
//   wire    [3:0] ball_reg, dig0, dig1;
//   assign ball_reg = 4'h2;
//   assign dig0 = 4'h3;
//   assign dig1 = 4'h5;
//   wire          text_pong_on;
//   wire    [7:0] text_pong_rgb8;
//   pong_text_harvey  PONG_TEXT
//      (.clk(Master_clk),
//       .ball(ball_reg), .dig0(dig0), .dig1(dig1),
//       .pix_x(pixel_x), .pix_y(pixel_y),
//       .text_on(text_pong_on), .text_rgb8(text_pong_rgb8));

//   reg [3:0]  scale5;
//   reg [3:0]  scale;
//   always @(negedge stopkey)
//     if (ascii_kb_tran==8'h85) // change scale on press of F1
//       begin
//         if (scale5 < 5) scale5 = scale5 +1; // select next scale
//         else scale5 = 4'h0;
//         case (scale5)
//              4'h0: scale = 4'h1; // scale  1x
//              4'h1: scale = 4'h2; // scale  2x
//              4'h2: scale = 4'h4; // scale  4x
//              4'h3: scale = 4'h8; // scale  8x
//              4'h4: scale = 4'hf; // scale 16x
//           default: scale = 4'h4; // scale  4x
//         endcase
//       end

//   // instantiate scale_text_ram module, needed wires first
//   // text array using RAM
//   // text array using fixed values
//   wire        text_ram3_on, text_scale_on;
//   wire [7:0]  text_ram3_rgb8, text_scale_rgb8;
//   scale_text_ram   TEXT_RAM_PONG
//   (.clk(Master_clk), .we(stopkey), .scale(scale),
//    .arrow(arrow), .ascii_in(ascii_kb_tran),
//    .pix_x(pixel_x), .pix_y(pixel_y),
//    .text_ram3_on(text_ram3_on), .text_ram3_rgb8(text_ram3_rgb8),
//    .text_scale_on(text_scale_on), .text_scale_rgb8(text_scale_rgb8));

//   // instantiate various-shaped rectangles module, needed wires first
//   wire    [7:0] rect_rgb8;
//   wire          misc_rect_on;
//   misc_rect15  MISC_RECT
//      (.Master_clk(Master_clk),
//       .pixel_x(pixel_x), .pixel_y(pixel_y),
//       .InputNumber(number8[0]),
//       .rect_on(misc_rect_on), .rect_rgb8(rect_rgb8));

   // instantiate ascii text module, needed wires first
   // ascii character display in 40 by 15 array like PONG
   wire    [7:0] text_40x15_rgb8P;
   wire          text_40x15_on;
   text_40x15  TEXT_40x15_case
      (.clk(Master_clk), .number(number8),
       .pix_x(pixel_x), .pix_y(pixel_y),
       .cur40x15_y_x(cur40x15_y_x),
       .text_4x_on(text_40x15_on), .text_4x_rgb8(text_40x15_rgb8P));

//   // instantiate Digilent Logo module, needed wires first
//   // Digilent Logo, a triangle that fills most of monitor
//   // or other ROM content, such as a PHOTO
//   wire    [2:0] outRed, outGreen;
//   wire    [1:0] outBlue;
//   wire   [11:0] AdrB;
//   wire    [7:0] dig_logo_rgb8;
//   wire          dig_logo_on;
//   assign AdrB = {pixel_y[8:3],pixel_x[8:3]}; // nearly full screen
//// Enable only one of these ROMs
////   VideoRom4bit  PHOTO
////      (.outRed(outRed), .outGreen(outGreen), .outBlue(outBlue),
////       .AdrB(AdrB));
//// or this ROM
////   VideoRom3bit  PHOTO
////      (.outRed(outRed), .outGreen(outGreen), .outBlue(outBlue),
////       .AdrB(AdrB));
//// or this ROM
////   VideoRomNoText  DIG_LOGO
////      (.outRed(outRed), .outGreen(outGreen), .outBlue(outBlue),
////       .AdrB(AdrB));
//// or this ROM
////   videoROM  PHOTO  // need to adjust outBlue to get best white and black
////      (.outRed(outRed), .outGreen(outGreen), .outBlue(outBlue),
////       .address(AdrB));
//// end of ROM choices
//   assign dig_logo_rgb8 = {outRed,outGreen,outBlue};
//   assign dig_logo_on = (!(3'b111 == {!outRed[2],outGreen[2],outBlue[0]})
//                         && !pixel_x[9]); // prevent display of second logo

   // instantiate boxes array module, needed wires first
   // 40_x by 30_y boxes array
   wire   [10:0] AdrG;
   wire          boxes_on;
   wire    [2:0] boxes_rgb3;
   wire    [7:0] boxes_rgb8;
   assign AdrG = {pixel_y[8:4],pixel_x[9:4]};
   box_40x30cpu4  BOXES_40x30
      (.address(AdrG), .boxes_on(boxes_on), .rgb3(boxes_rgb3), .rgb8(boxes_rgb8));
////   box_40x30_br  BOXES_40x30
////      (.clk(Master_clk), .address(AdrG), .boxes_on(boxes_on), .rgb3(boxes_rgb3), .rgb8(boxes_rgb8));


   //=======================================================
   // rgb multiplexing circuit
   // The first signal in "else if" list is top priority,
   // the second signal is next priority, and so on.
   // Change position of 2-line "else if" set to change priority
   // Resources used by a layer shown as "layer to display, 14%, 1BR"
   //=======================================================
   always @ (Master_clk) // could add items in sensitivity list to avoid warnings
//   always @ (Master_clk or ascii_4x_on or rect_01_on or rect_02_on or ascii_4x_rgb8P or rect_01_Color or rect_02_Color) //
   if (1'b1==1'b0) // do nothing except enable uniform "else if" statements
            begin end
//   else if (misc_rect_on)
//            rgb_next8 = rect_rgb8;       // layer to display, 11%, 0BR
   else if (text_40x15_on)
            rgb_next8 = text_40x15_rgb8P; // layer to display,  6%, 1BR ???
//   else if (text_ram3_on)
//            rgb_next8 = text_ram3_rgb8;  // layer to display, 17%, 3BR REF
//   else if (text_pong_on)
//            rgb_next8 = text_pong_rgb8;  // layer to display, 10%, 1BR
//   else if (text_scale_on)
//            rgb_next8 = text_scale_rgb8; // layer to display, 14%, 1BR
//   else if (dig_logo_on)
//            rgb_next8 = dig_logo_rgb8;   // layer to display, 26%, 0BR, DIG_LOGO
   else if (boxes_on)
            rgb_next8 = boxes_rgb8;      // layer to display, 16%, 0BR
   else // default is to display this
            rgb_next8 = 8'b001_001_01;   // blue-gray background, << 1%

   assign pre_RGB = rgb_next8;

endmodule
